import pymysql
import requests
import json

from dbutils.pooled_db import PooledDB


# 测试库
def getBetaConnection():
    pool = PooledDB(pymysql, 1, host='172.20.150.110', user='test_movie', passwd='movie20189933',
                db='cc_intelligent_recognition', port=4308)  # 1为连接池里的最少连接数
    conn = pool.connection()
    cur = conn.cursor()
    return conn, cur


##
def putText():
    conn, cur = getBetaConnection()
    try:
        # 获取总记录数
        select_sql = "select t.file_url,t.third_content_id from cc_recognition_result t where t.source = 'aigc_strict_test_07' and t.result !='NORMAL' order by t.recognition_id limit 100"

        cur.execute(select_sql)
        results = cur.fetchall()
        for result in results:
            text = result[0]
            third_content_id = result[1]
            url = "http://localhost:8080/v1/putText"

            payload = json.dumps({
                "thirdContentId": third_content_id,
                "source": "aigc_strict_test_07",
                "contentType": "text",
                "text": text,
                "isAgain": 1,
                "preset": "aigc_strict"
            })
            headers = {
                'Content-Type': 'application/json'
            }

            response = requests.request("POST", url, headers=headers, data=payload)

            # 打印响应内容（可以根据需要处理响应）
            # print(f'Response for thirdContentId {third_content_id}: {response.text}')
            print(f' thirdContentId :{third_content_id},text: {text},response {response.text}')

    finally:
        # 确保无论如何都关闭数据库连接
        cur.close()
        conn.close()


if __name__ == '__main__':
    putText();